﻿using System;
using System.Web.UI;

namespace Sencha.Direct.Api
{
	public class CacheParameters
	{
		// Summary:
		//     Gets or sets the cache profile name.
		//
		// Returns:
		//     The cache profile name.
		public string CacheProfile { get; private set; }
		//
		// Summary:
		//     Gets or sets the cache duration.
		//
		// Returns:
		//     The cache duration.
		public int Duration { get; private set; }
		//
		// Summary:
		//     Gets or sets the location.
		//
		// Returns:
		//     The location.
		public OutputCacheLocation Location { get; private set; }
		//
		// Summary:
		//     Gets or sets a value that indicates whether to store the cache.
		//
		// Returns:
		//     true if the cache should be stored; otherwise, false.
		public bool NoStore { get; private set; }
		//
		// Summary:
		//     Gets or sets the SQL dependency.
		//
		// Returns:
		//     The SQL dependency.
		public string SqlDependency { get; private set; }
		//
		// Summary:
		//     Gets or sets the vary-by-content encoding.
		//
		// Returns:
		//     The vary-by-content encoding.
		public string VaryByContentEncoding { get; private set; }
		//
		// Summary:
		//     Gets or sets the vary-by-custom value.
		//
		// Returns:
		//     The vary-by-custom value.
		public string VaryByCustom { get; private set; }
		//
		// Summary:
		//     Gets or sets the vary-by-header value.
		//
		// Returns:
		//     The vary-by-header value.
		public string VaryByHeader { get; private set; }
		//
		// Summary:
		//     Gets or sets the vary-by-param value.
		//
		// Returns:
		//     The vary-by-param value.
		public string VaryByParam { get; private set; }

		public CacheParameters(System.Web.Mvc.OutputCacheAttribute cacheAttribute)
		{
			if (cacheAttribute == null)
				throw new ArgumentNullException("cacheAttribute");

			this.CacheProfile = cacheAttribute.CacheProfile;
			this.Duration = cacheAttribute.Duration;
			this.Location = cacheAttribute.Location;
			this.NoStore = cacheAttribute.NoStore;
			this.SqlDependency = cacheAttribute.SqlDependency;
			this.VaryByContentEncoding = cacheAttribute.VaryByContentEncoding;
			this.VaryByCustom = cacheAttribute.VaryByCustom;
			this.VaryByHeader = cacheAttribute.VaryByHeader;
			this.VaryByParam = cacheAttribute.VaryByParam;
		}

		public override string ToString()
		{
			return string.Format("{0}, duration={1}s, {2}",
				this.Location,
				this.Duration,
				(this.NoStore ? "nostore " : "") +
				(this.VaryByContentEncoding != null ? "vary-by-content-encoding " : "") +
				(this.VaryByCustom != null ? "vary-by-custom " : "") +
				(this.VaryByHeader != null ? "vary-by-header " : "") +
				(this.VaryByParam != null ? "vary-by-param" : ""));
		}
	}
}
